<?php
/**
 * 创建模型类
 * 
 * 类名
 * ml_model_db...后面接自己的名字，驼峰写法
 *
 * 
 */
class ml_model_dbAct2atme extends Lib_datamodel_db 
{
    
/**
 * 创建构造函数
 *
 */
    function __construct()
    {
        /**
         * 加载数据库配置
         */
        $db_config = ml_factory::load_standard_conf('db');        //目前只有一个配置文件，所以
        /**
         * 构造函数
         * 参数：
         * 1，当前模型名称
         * 2，相关数据库配置
         */
        parent::__construct('act2atme' , $db_config['act2atme']);
    }
    protected function hook_after_fetch()
    {
        $data = $this->get_data();

        if(isset($data['target_info']))
        {
            $data['target_info'] = explode(',' , $data['target_info']);
            $this->set_data($data);
        }else
        {
            foreach($data as $key=>$val)
            {
                if(isset($val['target_info']))
                {
                    $val['target_info'] = explode(',' ,$val['target_info']);
                }
                $arr[$key]=$val;
            }
            $this->set_data($arr);
        }

        return true;
    }
    protected function hook_before_write($array)
    {
        if(isset($array['target_info']))
        {
            $array['target_info'] = implode(',' , $array['target_info']);
        }
        return $array;
    }

    public function add_act2atme($rid , $uid , $aAtmeRids)
    {
        if(!$this->init_db($uid, self::DB_MASTER))
            return false;
        
        $aInsert = array(
            'act_rid' => $rid,
            'uid' => $uid,
            'target_info' => $aAtmeRids
        );
        return $this->insert($aInsert);
    }
    
    public function get_act2atme($rid)
    {
        $uid = ml_tool_resid::resid2uid($rid);
        if(!$this->init_db($uid, self::DB_SLAVE ))
            return false;
        
        $sql = 'select * from '.$this->table.' where act_rid="'.$this->escape($rid).'"';
        return $this->fetch_row($sql);
    }
    
    public function del_act2atme($rid)
    {
    
    }
}
